//////////////////////////////////////////////////////////////////////
// Haar2DCUDA.cpp
//
// SHEN Fangyang
// me@shenfy.com
//
// Copyright (C) SHEN Fangyang, 2010, All rights reserved.
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
//  Brief Description
//
//	CUDA implementation of 2D Haar wavelet encoding/decoding,
//	host code file.
//
//////////////////////////////////////////////////////////////////////

#include <gpgpu_stdafx.h>
#include <Haar2DCUDA.h>
#include <cuda.h>
#include <ImgTmpl.h>

using namespace grflib::image;

CHaar2DCUDA::CHaar2DCUDA(void) :
	m_width(0),
	m_channel(0),
	m_pBuf(0),
	m_pdBuf1(0),
	m_pdBuf2(0)
{
}

CHaar2DCUDA::~CHaar2DCUDA(void)
{
	Release();
}

bool CHaar2DCUDA::Encode(w32lib::CImgTmpl<float> *pCoeff, const w32lib::CImgTmpl<float> *pImg)
{
	if (!pImg || !pCoeff)
		return false;

	//If size changed, re-initialize
	if (m_width != pImg->GetWidth() || m_width != pImg->GetHeight() || m_channel != pImg->GetBPP() / 8 / sizeof(float))
	{
		Init(pImg->GetWidth(), pImg->GetHeight(), pImg->GetBPP() / 8 / sizeof(float));
	}

	//Reset output buffer size if necessary
	pCoeff->SetSize(m_width, m_width, m_channel);

	//encode
	return CudaEncode(pCoeff->GetBuffer(), pImg->GetBuffer());
}

bool CHaar2DCUDA::Decode(w32lib::CImgTmpl<float> *pImg, const w32lib::CImgTmpl<float> *pCoeff)
{
	if (!pImg || !pCoeff)
		return false;

	//If size changed, re-initialize
	if (m_width != pCoeff->GetWidth() || m_width != pCoeff->GetHeight() || m_channel != pCoeff->GetBPP() / 8 / sizeof(float))
	{
		Init(pCoeff->GetWidth(), pCoeff->GetHeight(), pCoeff->GetBPP() / 8 / sizeof(float));
	}

	//Reset output buffer size if necessary
	pImg->SetSize(m_width, m_width, m_channel);

	//decode
	return CudaDecode(pImg->GetBuffer(), pCoeff->GetBuffer());
}